<template>
  <el-dialog :visible.sync="dialog" :close-on-click-modal="false" :before-close="cancel" :title="isAdd ? '新增用户' : '编辑用户'" width="570px">
    <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="66px">
      <el-form-item label="账号" prop="username">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input v-model="form.nickName" />
      </el-form-item>
      <el-form-item label="状态" prop="enabled">
        <el-radio-group v-model="form.enabled" style="width:184px;">
          <el-radio :label="true">激活</el-radio>
          <el-radio :label="false">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model.number="form.phone" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" />
      </el-form-item>
      <el-form-item label="部门" prop="deptId">
        <treeselect v-model="form.deptId" :load-options="getDepartments" :normalizer="normalizer" :options="departments" style="width: 184px" placeholder="选择部门" />
      </el-form-item>
      <!-- <el-form-item label="岗位">
        <el-select v-model="jobId" style="width: 178px" placeholder="请先选择部门">
          <el-option
            v-for="(item, index) in jobs"
            :key="item.name + index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item style="margin-bottom: 0px;" label="角色">
        <el-select v-model="roleIds" style="width: 450px;" multiple placeholder="请选择">
          <el-option
            v-for="(item, index) in roles"
            :key="item.name + index"
            :disabled="level !== 1 && item.level <= level"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item> -->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getDepartmentTree } from '@/api/system/department'
import { addUser, editUser } from '@/api/system/user'
import Treeselect from '@riophae/vue-treeselect'
import { LOAD_ROOT_OPTIONS } from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  components: { Treeselect },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      loading: false,
      dialog: false,
      departments: null,
      form: { username: '', nickName: '', enabled: true, phone: '', email: '', deptId: null },
      rules: {},
      normalizer(node) {
        // 去掉children为空的children属性
        if (!node.children || (node.children && !node.children.length)) {
          delete node.children
        }
        return {
          id: node.id,
          label: node.name,
          children: node.children
        }
      }
    }
  },
  mounted() {
  },
  methods: {
    async getDepartments({ action }) {
      if (action === LOAD_ROOT_OPTIONS) {
        await getDepartmentTree().then(res => {
          this.departments = []
          const department = { id: 0, name: '顶级部门', children: [] }
          department.children = res.data
          this.departments.push(department)
        })
      }
    },
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else {
            this.doEdit()
          }
        } else {
          return false
        }
      })
    },
    doAdd() {
      addUser(this.form).then(res => {
        this.resetForm()
        this.$notify({ title: '添加成功', type: 'success', duration: 2500 })
        this.$parent.init()
      }).finally(() => {
        this.loading = false
      })
    },
    doEdit() {
      editUser(this.form).then(res => {
        this.resetForm()
        this.$notify({ title: '修改成功', type: 'success', duration: 2500 })
        this.$parent.init()
      }).finally(() => {
        this.loading = false
      })
    },
    resetForm() {
      this.dialog = false
      this.form = { username: '', nickName: '', enabled: true, phone: '', email: '', deptId: null }
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /deep/ .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
